#### II Replication ####
### purpose: replicating table 1 and 2 ####
# installing packages ----

list.of.packages = c('estimatr', 'stargazer', 'MASS')
new.packages <- list.of.packages[!(list.of.packages %in% 
                                     installed.packages()[,"Package"])]

if(length(new.packages)) install.packages(new.packages)

# loading packages ----

suppressPackageStartupMessages(
  {
    library(estimatr)
    library(stargazer)
    library(MASS)
  }
)

# loading in data ----
dt <- readRDS("~/rep_data.RDS")

# Table 1 ----
lm1 <- lm(fascore ~ modi0_num_firm, data=dt)

lm1_se <- lm_robust(fascore ~ modi0_num_firm, data=dt, se_type = "HC2")

negbino1 <- glm.nb(fascore ~ modi0_num_firm, data = dt)

lm2 <- lm(fascore ~ modi0_num_firm + 
            highskill + highedu + 
            medianincome + unemploy + 
            black + white + hispanic + foreignborn
          +  EvanProt + Catholic + MainProt
          + nominate1 + app + foreign  + 
            labor + corp + bank + prezSupport 
          + as.factor(state), data = dt)

lm2_se <- lm_robust(fascore ~ modi0_num_firm + 
                      highskill + highedu + 
                      medianincome + unemploy + 
                      black + white + hispanic + foreignborn
                    +  EvanProt + Catholic + MainProt
                    + nominate1 + app + foreign  + 
                      labor + corp + bank + prezSupport, fixed_effects = ~ as.factor(state), data=dt, clusters = as.factor(state), se_type = "CR2")

negbino2 <- glm.nb(fascore ~ modi0_num_firm + 
                     highskill + highedu + 
                     medianincome + unemploy + 
                     black + white + hispanic + foreignborn
                   +  EvanProt + Catholic + MainProt
                   + nominate1 + app + foreign  + 
                     labor + corp + bank + prezSupport 
                   + as.factor(state), data = dt)

lm3 <- lm(fascore ~ modi0_num_firm + 
            highskill + highedu + 
            medianincome + unemploy + 
            black + white + hispanic + foreignborn
          +  EvanProt + Catholic + MainProt
          + nominate1 + app + foreign  + 
            labor + corp + bank + prezSupport 
          + as.factor(state) + as.factor(cong), 
          data = dt)

lm3_se <- lm_robust(fascore ~ modi0_num_firm + 
                      highskill + highedu + 
                      medianincome + unemploy + 
                      black + white + hispanic + foreignborn
                    +  EvanProt + Catholic + MainProt
                    + nominate1 + app + foreign  + 
                      labor + corp + bank + prezSupport, fixed_effects = ~ state + cong, data=dt, 
                    clusters = state, se_type = "CR2")


negbino3 <- glm.nb(fascore ~ modi0_num_firm + 
                     highskill + highedu + 
                     medianincome + unemploy + 
                     black + white + hispanic + foreignborn
                   +  EvanProt + Catholic + MainProt
                   + nominate1 + app + foreign  + 
                     labor + corp + bank + prezSupport 
                   + as.factor(state) + as.factor(cong), 
                   data = dt)

stargazer(lm1, lm2,  lm3, negbino1, negbino2, negbino3,
          model.names =TRUE,
          se = starprep(lm1_se, lm2_se, lm3_se),
          header=FALSE,
          keep=c("modi0_num_firm", 
                 "highskill", "highedu",
                 "medianincome", "unemploy",
                 "black", "white", "hispanic",
                 "foreignborn",
                 "EvanProt","Catholic", "MainProt",
                 "nominate1", "app", "foreign", 
                 "labor", "corp", "bank",
                 "prezSupport"),
          covariate.labels=c("DevFirm Num",
                             "HighSkill", 
                             "HighEdu",
                             " ln(MedianIncome)", 
                             "Unemploy", 
                             "Black", 
                             "White", 
                             "Hispanic", 
                             "ForeignBorn",
                             "EvanProt", 
                             "Catholic",  
                             "MainProt", 
                             "Nominate1",
                             "ApproprtnCommt",
                             "ForeignCommt", 
                             "LaborPAC", 
                             "CorpPAC", 
                             "BankPAC",
                             "PrezSupport"), 
          align=T,
          font.size="scriptsize",
          omit.stat=c("f", "ser", "bic", "aic", "ll", "theta"),
          column.sep.width="0pt",
          dep.var.labels = "Dependent Variable: FA Score (The number of pro-foreign aid bills each representative cosponsored)",
          title = "Results", 
          omit.table.layout="l",
          type="latex",
          add.lines = list( c("state FE", "N", "Y",  "Y",  "N", "Y", "Y"), c("cong FE", "N",  "N", "Y", "N", "N", "Y"), c("SE cluster", "-",  "state", "state", "-", "-", "-")))


# Table 2 ----
## First Row ----
summary(dt$modi0_num_firm)

## Second Row ----
used <- lm2[["model"]]
summary(used$modi0_num_firm)